package cn.fan.study.five;


import entity.SubscribeReqProto;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 服务处理类
 *
 * @author HTHLKJ
 * @version 1.0
 * @date 2021/8/5 15:42
 */
@Slf4j
public class SubReqServerHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        entity.SubscribeReqProto.SubscribeReq body = (entity.SubscribeReqProto.SubscribeReq) msg;

        if ("Lilinfeng".equalsIgnoreCase(body.getUserName())) {
            log.info("接收到客户端：{}", body.toString());
        }
        ctx.writeAndFlush(resp(body.getSubReqID()));
    }

    private entity.SubscribeRespProto.SubscribeResp resp(int subReqID) {
        entity.SubscribeRespProto.SubscribeResp.Builder builder = entity.SubscribeRespProto.SubscribeResp.newBuilder();
        builder.setSubReqID(subReqID);
        builder.setRespCode(0);
        builder.setDesc("Netty book order succeed, 3 days later, sent to the designated address");
        return builder.build();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
